package edu.ujf.sharepas.impl;
// Client distant, a lancer apres le serveur


import java.io.Serializable;
import java.rmi.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.List;
import java.util.Observer;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.part.ViewPart;
import edu.ujf.sharepas.rcp.ChatView;
import edu.ujf.sharepas.rmi.Client;
import edu.ujf.sharepas.rmi.Serveur;
      
public class ClientImpl extends UnicastRemoteObject implements Client,Serializable {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = -7310128765812703765L;
	
	private Chat chat ;
	private Serveur s ;
	private LocalUser user;
	
	
	public ClientImpl() throws RemoteException {
		super(); 
		try{		
			String nomServeur =null;
			String listenAddr = null;
			// nom de l'objet du serveur distant lie au registre rmi
			if( Constante.getSharePasProperty(Constante.Property.SERVER_ADDR.toString()) != null ){
				nomServeur = Constante.getSharePasProperty(Constante.Property.SERV_NAME.toString());
				listenAddr = Constante.getSharePasProperty(Constante.Property.SERVER_ADDR.toString());
			}else{
				nomServeur = "//localhost/ServMessage";	
				listenAddr = "localhost";
			}
			
		
			MySecurityManager mySm = new MySecurityManager(); 
			System.setSecurityManager(mySm);
			Registry reg = LocateRegistry.getRegistry(listenAddr);
			// recherche de l'objet distant Serveur dans le rmiregistry
			this.s  = (Serveur) reg.lookup(nomServeur);
			System.out.println("serveur "+this.s.toString());
		
		} catch (Exception e){
			System.out.println("Error while performing RMI"+e);
			e.printStackTrace();
		}
		
		//this.setUser(new User(this,"anonymous"+((int)(Math.random()*100000))));
		this.setUser(new LocalUser(this));
		
	}

	public void addChat(Chat c) throws RemoteException{
		this.chat = c;
	}
	

	public void send(Message m) throws RemoteException{
		this.message(m);
	}

	
	@Override
	public void connect() throws RemoteException {
	
		user.setState(Constante.UserState.CONNECTED);
		this.s.connect(this);
		
		
	}

	@Override
	public void disconnect() throws RemoteException {
		user.setState(Constante.UserState.DISCONNECTED);
		this.s.disconnect(this);
	}


	@Override
	public void message(final Message m) throws RemoteException {
		if(chat != null){
			chat.setMessage(m);
		}
	}


	@Override
	public Serveur getServeur() throws RemoteException {
		return s;
	}




	public void setUser(LocalUser user) {

		this.user = user;
	}

	@Override
	public LocalUser getUser() throws RemoteException {
		return user;
	}

	@Override
	public void updateFriends(final List<User> listServer) throws RemoteException {
		Display.getDefault().asyncExec(new Runnable(){
			@Override
			public void run() {
				((LocalUser)user).updateFriends(listServer);
			}
		});
		
	}

	@Override
	public ShareFile getShareFile(String fileName) throws RemoteException {
		// TODO Auto-generated method stub
		return ((LocalUser)this.user).getShareFile(fileName);
	}


}
